package GUI.gameWin;

import game.Player;
import gamePlus.client.ClientRoundUnit;

import java.awt.Color;
import java.awt.Dimension;
import java.util.LinkedList;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import GUI.gameWinOld.CardPanel;

import carddeck.Card;

public class CardPanelPlus extends CardPanel {
	private Player playerOwner;
	private ClientRoundUnit roundUnit;

	private boolean horizontal;

	public CardPanelPlus(ClientRoundUnit roundUnit, Player player, boolean bHorizontal) {
		super();
		this.roundUnit=roundUnit;
		playerOwner = player;
		setBackground(new Color(0, 77, 0));
		//setLayout(null);
		setBorder(new TitledBorder(playerOwner.getName()));
		((TitledBorder) getBorder()).setTitleColor(Color.WHITE);
		horizontal=bHorizontal;
		if(horizontal) {
			setLayout(new CardHorizontalLayout(15));
			setSize(new Dimension(540,100));
			setMinimumSize(new Dimension(540,100));
		}
		else {
			setLayout(new CardVerticalLayout(15));
			setSize(new Dimension(100, 540));
			setMinimumSize(new Dimension(100, 540));
		}
		setVisible(true);
		setOpaque(true);
	}

	public void refresh() {
		removeAll();
		LinkedList<Card> playerCards = playerOwner.getCards();
		for (int i = 0; i<playerCards.size()-1;i++) {
			Card theCard = playerCards.get(i);
			CardButtonPlus theButton = new CardButtonPlus(playerOwner, roundUnit, theCard, false, horizontal);
			add(theButton);
		}
		if(!playerCards.isEmpty()) {
			Card theCard = playerCards.getLast();
			CardButtonPlus theButton = new CardButtonPlus(playerOwner, roundUnit, theCard, true, horizontal);
			add(theButton);
		}
		repaint();
		revalidate();
	}
	
	public void select() {
		if(playerOwner.getWaitTime()>0)
			return;
		setBackground(new Color(0, 145, 0));
		repaint();
	}
	
	public void deselect() {
		if(playerOwner.getWaitTime()>0)
			return;
		setBackground(new Color(0, 77, 0));
		repaint();
	}
	public void setWaitTime(int waitTime) {
		if(waitTime>0) {
			setBackground(new Color(0, 0, 0));
			((TitledBorder) getBorder()).setTitle(playerOwner.getName()+" (czeka "+waitTime+")");
		}
		else if(waitTime<=0)
		{
			setBackground(new Color(0, 77, 0));
			((TitledBorder) getBorder()).setTitle(playerOwner.getName());
		}
		repaint();
	}
}
